// pages/search/search.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    positionId:0,
    companyId:0,
    searchName:"",
    wxSearchData:{},
    basename:"公司",
    psearch:0,
    csearch:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pid = options.pid;
    var cid=options.cid;
    //index search value
    var psearch=options.psearch;
    var csearch=options.csearch;
    var that=this;
    if(pid>0){
      that.setData({
        positionId: pid,
        psearch: psearch,
        basename: "职位"
      });
    }else if(cid>0){
      that.setData({
        companyId: cid,
        csearch:csearch,
        basename: "公司"
      });
    }
  },
 

  searchNameTap:function(e){
    var eValue = e.detail.value;
    this.setData({
      searchName: eValue
    })
    var that=this;
    var name=that.data.searchName;
    util.post(app.api.getSearchResult, { searchName: name, pid: that.data.positionId, cid: that.data.companyId }, function (res) {
      that.setData({
        wxSearchData:res.data
      });
    });
  },
  searchW:function(event){
    var name=this.data.searchName;
   if(this.data.positionId>0){
     if (this.data.searchName) {
       wx.navigateTo({
         url: '/pages/searchWork/index?name=' + name,
       })
     }
   }else if(this.data.companyId>0){
     if(this.data.searchName){
       wx.navigateTo({
         url: '/pages/searchCop/index?name=' + name,
       })
     }
   }
    
  },

  wxSearchKeyTap:function(event){
    var that=this;
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    var prevPage = pages[pages.length - 2];
    if (that.data.positionId > 0) {
      //职位
      if (that.data.psearch>0){
        wx.navigateTo({
          url: '/pages/searchJob/index?name=' +event.currentTarget.dataset.name,
        })
      }else{
        prevPage.setData({
          searchPosiValue: {
            id: event.currentTarget.dataset.key,
            name: event.currentTarget.dataset.name
          }
        })
        wx.navigateBack({
          delta: 1
        })
      }
     
    } else if (that.data.companyId > 0) {
      if (that.data.csearch > 0) {
        wx.navigateTo({
          url: '/pages/searchCom/index?name=' + event.currentTarget.dataset.name,
        })
      } else {
      //公司
      prevPage.setData({
        searchValue: {
          id: event.currentTarget.dataset.key,
          name: event.currentTarget.dataset.name
        }
      })
      wx.navigateBack({
        delta: 1
      })
    }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})